var labelType, useGradients, nativeTextSupport, animate;

(function(){
  var ua = navigator.userAgent,
      iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
      typeOfCanvas = typeof HTMLCanvasElement,
      nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
      textSupport = nativeCanvasSupport 
        && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
  //I'm setting this based on the fact that ExCanvas provides text support for IE
  //and that as of today iPhone/iPad current text support is lame
  labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
  nativeTextSupport = labelType == 'Native';
  useGradients = nativeCanvasSupport;
  animate = !(iStuff || !nativeCanvasSupport);
})();

var Log = {
  elem: false,
  write: function(text){
    if (!this.elem) 
      this.elem = document.getElementById('log');
    this.elem.innerHTML = text;
    this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
  }
};


function init(){
    //init data
    var json = {
        "id": "347_0",
        "name": "penicillin",
        "children": [
            {
            "id": "126510_1",
            "name": "抗性基因",
            "data": {
                "band": "抗性基因",
                "relation": "resistence gene"
            },
            "children": [{
                "id": "MecI",
                "name": "MecI",
                "data": {
                    "band": "MecI",
                    "relation": "member of band"
                },
                "children": []
            }, {
                "id": "ccrA1",
                "name": "ccrA1",
                "data": {
                    "band": "MecI",
                    "relation": "member of band"
                },
                "children": []
            }]
        }, 
        {
            "id": "173871_4",
            "name": "研究机构",
            "data": {
                "band": "Nine Inch Nails",
                "relation": "member of band"
            },
            "children": [
                {
                    "id": "北京大学",
                    "name": "北京大学",
                    "data": {
                        "band": "北京大学",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "中国科学院微生物研究所",
                    "name": "中国科学院微生物研究所",
                    "data": {
                        "band": "中国科学院微生物研究所",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "上海交通大学",
                    "name": "上海交通大学",
                    "data": {
                        "band": "上海交通大学",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "华中科技大学",
                    "name": "华中科技大学",
                    "data": {
                        "band": "华中科技大学",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "四川大学",
                    "name": "四川大学",
                    "data": {
                        "band": "四川大学",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "北京科技大学",
                    "name": "北京科技大学",
                    "data": {
                        "band": "北京科技大学",
                        "relation": "member of band"
                    },
                    "children": []
                }
            ]
        },{
            "id": "person",
            "name": "研究人员",
            "data": {
                "band": "Nine Inch Nails",
                "relation": "member of band"
            },
            "children": [
                {
                    "id": "王静康",
                    "name": "王静康",
                    "data": {
                        "band": "王静康",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "段雪",
                    "name": "段雪",
                    "data": {
                        "band": "pbp3",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "魏江春",
                    "name": "魏江春",
                    "data": {
                        "band": "魏江春",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "邓子新",
                    "name": "邓子新",
                    "data": {
                        "band": "邓子新",
                        "relation": "member of band"
                    },
                    "children": []
                }
            ]
        } 
        ,{
            "id": "235952_5",
            "name": "结合蛋白基因",
            "data": {
                "band": "Nine Inch Nails",
                "relation": "member of band"
            },
            "children": [
                {
                    "id": "prfA",
                    "name": "prfA",
                    "data": {
                        "band": "prfA",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "pbp3",
                    "name": "pbp3",
                    "data": {
                        "band": "pbp3",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                    "id": "pbp4",
                    "name": "pbp4",
                    "data": {
                        "band": "pbp4",
                        "relation": "member of band"
                    },
                    "children": [{
                    "id": "2382_7",
                    "name": "Achromobacter sp. CCM 4824",
                    "data": {
                        "band": "Achromobacter sp. CCM 4824",
                        "relation": "member of band"
                    },
                    "children": []
                },{
                    "id": "魏江春",
                    "name": "魏江春",
                    "data": {
                        "band": "魏江春",
                        "relation": "member of band"
                    },
                    "children": []
                }]
                }
            ]
        }, {
            "id": "235951_6",
            "name": "产生菌",
            "data": {
                "band": "producing strain",
                "relation": "member of band"
            },
            "children": [
                {
                    "id": "2382_7",
                    "name": "Achromobacter sp. CCM 4824",
                    "data": {
                        "band": "Achromobacter sp. CCM 4824",
                        "relation": "member of band"
                    },
                    "children": []
                }, 
                {
                    "id": "2415_8",
                    "name": "Penicillium chrysogenum",
                    "data": {
                        "band": "Penicillium chrysogenum",
                        "relation": "member of band"
                    },
                    "children": []
                }, 
                {
                    "id": "3963_9",
                    "name": "Escherichia coli",
                    "data": {
                        "band": "Escherichia coli",
                        "relation": "member of band"
                    },
                    "children": []
                },
                {
                "id": "产黄青霉",
                "name": "Penicillium chrysogenum(产黄青霉)",
                "data": {
                    "band": "MecI",
                    "relation": "member of band"
                },
                "children": []
            }, {
                "id": "特异青霉",
                "name": "Penicillium notatum(特异青霉)",
                "data": {
                    "band": "MecI",
                    "relation": "member of band"
                },
                "children": []
            },
            {
                "id": "链霉菌属菌种",
                "name": "Streptomyces sp.(链霉菌属菌种)",
                "data": {
                    "band": "MecI",
                    "relation": "member of band"
                },
                "children": []
            },
             {
                "id": "Streptomyces microflavus",
                "name": "Streptomyces microflavus",
                "data": {
                    "band": "MecI",
                    "relation": "member of band"
                },
                "children": []
            }
                ]
        }],
    };
    //end
    var infovis = document.getElementById('infovis');
    var w = infovis.offsetWidth - 50, h = infovis.offsetHeight - 50;
    
    //init Hypertree
    var ht = new $jit.Hypertree({
      //id of the visualization container
      injectInto: 'infovis',
      Navigation: {  
        enable: true,  
        panning: true,  
        zooming: 20  
        }, 
      //canvas width and height
      width: w,
      height: h,
      //Change node and edge styles such as
      //color, width and dimensions.
      Node: {
          dim: 9,
          color: "#74d3fa"
      },
      Edge: {
          lineWidth: 1,
          color: "#ccc"
      },
      onBeforeCompute: function(node){
          
      },
      //Attach event handlers and add text to the
      //labels. This method is only triggered on label
      //creation
      onCreateLabel: function(domElement, node){
          domElement.innerHTML = node.name;
          $jit.util.addEvent(domElement, 'click', function () {
              ht.onClick(node.id, {
                  onComplete: function() {
                      ht.controller.onComplete();
                  }
              });
          });
      },
      //Change node styles when labels are placed
      //or moved.
      onPlaceLabel: function(domElement, node){
          var style = domElement.style;
          style.display = '';
          style.cursor = 'pointer';
          if (node._depth <= 1) {
              style.fontSize = "1.5em";
              style.color = "#940511";

          } else if(node._depth == 2){
              style.fontSize = "0.5em";
              style.color = "#555";

          } else {
              style.display = 'none';
          }

          var left = parseInt(style.left);
          var w = domElement.offsetWidth;
          style.left = (left - w / 2) + 'px';
      },
      
      onComplete: function(){
          
          
          //Build the right column relations list.
          //This is done by collecting the information (stored in the data property) 
          //for all the nodes adjacent to the centered node.
          var node = ht.graph.getClosestNodeToOrigin("current");
          var html = "<h4>" + node.name + "</h4><b>Connections:</b>";
          html += "<ul>";
          node.eachAdjacency(function(adj){
              var child = adj.nodeTo;
              if (child.data) {
                  var rel = (child.data.band == node.name) ? child.data.relation : node.data.relation;
                  html += "<li>" + child.name + " " + "<div class=\"relation\">(relation: " + rel + ")</div></li>";
              }
          });
          html += "</ul>";
          $jit.id('inner-details').innerHTML = html;
      }
    });
    //load JSON data.
    ht.loadJSON(json);
    //compute positions and plot.
    ht.refresh();
    //end
    ht.controller.onComplete();
    ht.fx.animate({   
        duration: 100 
    }); 
}
